package com.todo.biz.application.handler.bizData;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.todo.biz.application.entity.bo.BizCounterBO;
import com.todo.biz.application.entity.context.SimpleBusinessDataContext;
import com.todo.biz.application.remote.RestaurantInfoRemoteService;
import com.todo.biz.application.service.BizCounterService;
import com.todo.biz.common.enums.CounterTypeEnum;
import com.todo.common.common.entity.TodoPlatformUserModel;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BusinessSceneEnum;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.delicacy.service.api.request.QueryRestaurantInfoByIdListRequest;
import com.todo.delicacy.service.api.response.RestaurantInfoResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2024/5/30 11:12
 * @description
 */
@Slf4j
@Component
public class FoodDataHandler extends AbstractBusinessDataHandle {

    @Resource
    private RestaurantInfoRemoteService restaurantInfoRemoteService;
    @Resource
    private BizCounterService bizCounterService;


    @Override
    protected Map<Long, SimpleBusinessDataContext> doQueryBizData(List<Long> bizIdList) {
        if (CollectionUtils.isEmpty(bizIdList)) {
            return Maps.newHashMap();
        }
        QueryRestaurantInfoByIdListRequest request = new QueryRestaurantInfoByIdListRequest();
        request.setRestaurantIdList(bizIdList);
        BizResponse<List<RestaurantInfoResponse>> restaurantInfoResponses = restaurantInfoRemoteService.queryRestaurantInfoByIdList(request);
        if (restaurantInfoResponses == null || !restaurantInfoResponses.isSuccess()  || CollectionUtils.isEmpty(restaurantInfoResponses.getData())) {
            log.warn("FoodDataHandler#doQueryBizData 根据主键查询内容详情完成，数据为空 入参：【{}】", JSONObject.toJSONString(bizIdList));
            return Maps.newHashMap();
        }

        Map<Long, SimpleBusinessDataContext> resultMap = Maps.newHashMap();
        for (RestaurantInfoResponse restaurantInfoResponse : restaurantInfoResponses.getData()) {
            //目前美食到店都是平台信息，所以默认填充用户
            TodoPlatformUserModel platformUserModel = TodoCommonUtils.getPlatformUserModel();
            SimpleBusinessDataContext context = new SimpleBusinessDataContext();
            context.setBizId(String.valueOf(restaurantInfoResponse.getRestaurantId()));


            List<BizCounterBO> bizCounterBOList = bizCounterService.queryBizCounterByBizIdAndScene(restaurantInfoResponse.getRestaurantId(), BusinessSceneEnum.FOOD);
            if (CollectionUtils.isNotEmpty(bizCounterBOList)) {
                Map<CounterTypeEnum, Integer> collectMap = bizCounterBOList.stream().collect(Collectors.toMap(BizCounterBO::getCounterType, BizCounterBO::getCount, (k1, k2) -> k1));
                context.setLikeCount(collectMap.get(CounterTypeEnum.LIKE) == null ? 0 : collectMap.get(CounterTypeEnum.LIKE));
                context.setCommentCount(collectMap.get(CounterTypeEnum.COMMENT) == null ? 0 : collectMap.get(CounterTypeEnum.COMMENT));
                context.setCollectCount(collectMap.get(CounterTypeEnum.COLLECT) == null ? 0 : collectMap.get(CounterTypeEnum.COLLECT));
            }

            SimpleBusinessDataContext.UserInfo userInfo = new SimpleBusinessDataContext.UserInfo();
            userInfo.setUserId(String.valueOf(platformUserModel.getUserId()));
            userInfo.setUserNikeName(platformUserModel.getUserNikeName());
            userInfo.setUserAvatar(platformUserModel.getUserAvatar());
            userInfo.setLocation(platformUserModel.getLocation());
            context.setUserInfo(userInfo);
            context.setTitle(restaurantInfoResponse.getName());
            context.setMainImg(restaurantInfoResponse.getMainImg());
            context.setSummary(restaurantInfoResponse.getDesc() == null ? "" : restaurantInfoResponse.getDesc().substring(0, 100));
            context.setBusinessScene(BusinessSceneEnum.PLATFORM_STRATEGY);
            resultMap.put(restaurantInfoResponse.getRestaurantId(), context);
        }
        return resultMap;
    }

    @Override
    public BusinessSceneEnum getHandleBusinessScene() {
        return BusinessSceneEnum.FOOD;
    }
}
